//package com.example.rbac.mq.send;
//
//import com.example.rbac.entity.Message;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.rabbit.support.CorrelationData;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.MessageHandler;
//import org.springframework.messaging.MessageHeaders;
//import org.springframework.messaging.support.MessageBuilder;
//import org.springframework.stereotype.Service;
//
//import java.util.UUID;
//
//@Service
//public class SendHandle implements SendHandleInterface {
//
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    public static final Logger log = LoggerFactory.getLogger(SendHandle.class);
//
//    //根据队列名称
//    @Override
//    public Object send(Message message, String routingKey) {
//        rabbitTemplate.setConfirmCallback(this);
//        rabbitTemplate.setReturnCallback(this);
//        MessageHeaders messageHeaders = new MessageHeaders(message.getMessage());
//        org.springframework.messaging.Message<Message> msg = MessageBuilder.createMessage(message, messageHeaders);
//        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
//        if (message.isSync()) {
//            //发布同步消息
//            String res = rabbitTemplate.convertSendAndReceive(routingKey, message, correlationData).toString();
//            log.info(res);
//            return res;
//        } else {
//            rabbitTemplate.convertAndSend(routingKey, msg, correlationData);
//            return null;
//        }
//    }
//
//    //根据交换器名称发送消息
//    @Override
//    public Object sendFanout(Message message, String exchange) {
//        rabbitTemplate.setConfirmCallback(this);
//        rabbitTemplate.setReturnCallback(this);
//        String routingKey = message.getRoutingKey();
//        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
//        if (message.isSync()) {
//            //发布同步消息
//            String res = rabbitTemplate.convertSendAndReceive(exchange, routingKey, message, correlationData).toString();
//            log.info(res);
//            return res;
//        } else {
//            rabbitTemplate.convertAndSend(exchange, message, correlationData);
//            return null;
//        }
//    }
//
//    //查看消息是否发送成功
//    @Override
//    public void confirm(CorrelationData correlationData, boolean b, String s) {
//        String str = correlationData.getId();
//        if (b) {
//            log.info("消息发送成功");
//        } else {
//            log.info("消息发送失败");
//        }
//    }
//
//    @Override
//    public void returnedMessage(org.springframework.amqp.core.Message message, int i, String s, String s1, String s2) {
//        System.out.println("消息返回成功");
//    }
//}
